package com.zoweunion.mechanic.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.dao.TSSsWarehouseRegionMapper;
import com.zoweunion.mechanic.dao.TSSsWarehouselocationMapper;
import com.zoweunion.mechanic.entity.TSSsWarehouselocationEntity;
import com.zoweunion.mechanic.entity.vo.TSSsWarehouseRegionVO;
import com.zoweunion.mechanic.entity.vo.TSSsWarehouselocationVO;
import com.zoweunion.mechanic.model.app.AssignRegion;
import com.zoweunion.mechanic.service.ITSSsWarehouselocationService;
import com.zoweunion.mechanic.util.FunUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TSSsWarehouselocationServiceImpl extends ServiceImpl<TSSsWarehouselocationMapper, TSSsWarehouselocationEntity> implements ITSSsWarehouselocationService {

    @Autowired
    private TSSsWarehouselocationMapper tSSsWarehouselocationMapper;

    @Autowired
    private TSSsWarehouseRegionMapper warehouseRegionDao;


    @Override
    public PageInfo listPage(Map<String, Object> map) {

        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
        Integer currentPage = Integer.valueOf(map.get("currentPage").toString());
        PageHelper.startPage(currentPage, pageSize);

        List<TSSsWarehouselocationVO> mapList = tSSsWarehouselocationMapper.findByCondition(map);
        PageInfo<TSSsWarehouselocationVO> mapPageInfo = new PageInfo<>(mapList);
        return mapPageInfo;
    }


    @Override
    public TSSsWarehouselocationEntity detail(String id) {

        TSSsWarehouselocationEntity tSSsWarehouselocation = this.getById(id);

        Map<String,Object> map = new HashMap<>();
        map.put("warehouseId",tSSsWarehouselocation.getId());
        List<TSSsWarehouseRegionVO> provinceList = warehouseRegionDao.findByCondition(map);

//        List<Map<String,Object>> provinceMapList = FunUtil.objectListToMapList(provinceList);

        tSSsWarehouselocation.setProvinceList(this.wrapTreeWithAssignRegion(provinceList));

        return tSSsWarehouselocation;
    }

    /**
     * 把员工负责区域数据封装为tree结构
     * @param assignRegionList
     * @return
     */
    private List<Map<String, Object>> wrapTreeWithAssignRegion(List<TSSsWarehouseRegionVO> assignRegionList) {
        final String key = "key", title = "title", children = "children";
        List<Map<String, Object>> provinceList = new ArrayList<>();
        for (TSSsWarehouseRegionVO assignRegion : assignRegionList) {
            Map<String, Object> provinceMap = this.getMapByNumber(provinceList, assignRegion.getProvinceNumber());
            if (provinceMap == null) {
                provinceMap = new HashMap<>(5);
                provinceMap.put(key, assignRegion.getProvinceNumber());
                provinceMap.put(title, assignRegion.getProvinceName());
                provinceList.add(provinceMap);
            }
            List<Map<String, Object>> cityList;
            if (provinceMap.get(children) != null) {
                cityList = (List<Map<String, Object>>) provinceMap.get(children);
            } else {
                cityList = new ArrayList<>(5);
                provinceMap.put(children, cityList);
            }
            Map<String, Object> cityMap = this.getMapByNumber(cityList, assignRegion.getCityNumber());
            if (cityMap == null) {
                cityMap = new HashMap<>();
                cityMap.put(key, assignRegion.getCityNumber());
                cityMap.put(title, assignRegion.getCityName());
                cityList.add(cityMap);
            }
            List<Map<String, Object>> areaList;
            if (cityMap.get(children) != null) {
                areaList = (List<Map<String, Object>>) cityMap.get(children);
            } else {
                areaList = new ArrayList<>(5);
                cityMap.put(children, areaList);
            }
            Map<String, Object> areaMap = this.getMapByNumber(areaList, assignRegion.getCityNumber());
            if (areaMap == null) {
                areaMap = new HashMap<>();
                areaMap.put(key, assignRegion.getAreaNumber());
                areaMap.put(title, assignRegion.getAreaName());
                areaList.add(areaMap);
            }
        }
        return provinceList;
    }

    private Map<String, Object> getMapByNumber (List<Map<String, Object>> addressList, String number) {
        for (Map<String, Object> map : addressList) {
            if (map.get("key").toString().equals(number)) {
                return map;
            }
        }
        return null;
    }


//    @Override
//    public List<TSSsWarehouselocationExcel> listExport(Map<String, Object> params) throws InvocationTargetException, IllegalAccessException {
//
//
//        PageHelper.startPage(1, 10);
//
//        List<TSSsWarehouselocationVO> tSSsWarehouselocationVOS = tSSsWarehouselocationMapper.findByCondition(params);
//
//        // 转化格式
//        List<TSSsWarehouselocationExcel> excels = new ArrayList<>();
//        TSSsWarehouselocationExcel excel = new TSSsWarehouselocationExcel();
//        for (TSSsWarehouselocationVO item : tSSsWarehouselocationVOS) {
//            BeanUtils.copyProperties(excel, item);
//            excels.add(excel);
//        }
//
//        return excels;
//    }


}

